// -----------------------------------------------------------------------
// <copyright file="WorkflowProfileEN.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    
    /// <summary>
    /// Class WorkflowProfile
    /// </summary>
    public class WorkflowProfileEN : EntityEN
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowProfileEN" /> class
        /// </summary>
        public WorkflowProfileEN()
        {
            this.WorkflowProfileId = 0;
            this.ProfileId = 0;
            this.WorkflowId = 0;
            this.NodeId = 0;
            this.SendUserId = 0;
            this.MainUserId = 0;
            this.ProgressWorkId = 0;
            this.SendDepartmentId = 0;
            this.MainDepartmentId = 0;
            this.IsCurrent = false;
            this.StartDate = null;
            this.EndDate = null;
            this.Role = string.Empty;
            this.RW = false;
        }
        
        /// <summary>
        /// Gets or sets WorkflowProfileId
        /// </summary>
        public int WorkflowProfileId { get; set; }

        /// <summary>
        /// Gets or sets SendDepartmentId
        /// </summary>
        public int SendDepartmentId { get; set; }

        /// <summary>
        /// Gets or sets MainDepartmentId
        /// </summary>
        public int MainDepartmentId { get; set; }
        
        /// <summary>
        /// Gets or sets ProfileId
        /// </summary>
        public int ProfileId { get; set; }
        
        /// <summary>
        /// Gets or sets WorkflowId
        /// </summary>
        public int WorkflowId { get; set; }
        
        /// <summary>
        /// Gets or sets NodeId
        /// </summary>
        public int NodeId { get; set; }

        /// <summary>
        /// Gets or sets SendUserId
        /// </summary>
        public int SendUserId { get; set; }

        /// <summary>
        /// Gets or sets MainUserId
        /// </summary>
        public int MainUserId { get; set; }
        
        /// <summary>
        /// Gets or sets ProgressWorkId
        /// </summary>
        public int ProgressWorkId { get; set; }
        
        /// <summary>
        /// Gets or sets a value indicating whether the item is current
        /// </summary>
        public bool IsCurrent { get; set; }
        
        /// <summary>
        /// Gets or sets StartDate
        /// </summary>
        public DateTime? StartDate { get; set; }
        
        /// <summary>
        /// Gets or sets EndDate
        /// </summary>
        public DateTime? EndDate { get; set; }
        
        /// <summary>
        /// Gets or sets Role
        /// </summary>
        public string Role { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether RW
        /// </summary>
        public bool RW { get; set; }

        /// <summary>
        /// Gets Profile
        /// </summary>
        public ProfileEN Profile
        {
            get
            {
                try
                {
                    ProfileEN profile = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileGetById, this.ProfileId);

                    while (reader.Read())
                    {
                        profile = new ProfileEN();

                        if (!reader.IsDBNull(0))
                        {
                            profile.ProfileId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            profile.ProfileStatusId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            profile.ProfileTypeId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            profile.LevelId = reader.GetInt16(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            profile.StorageLocationProfileListId = reader.GetInt32(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            profile.CreateUserId = reader.GetInt32(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            profile.Code = reader.GetString(6);
                        }

                        if (!reader.IsDBNull(7))
                        {
                            profile.Name = reader.GetString(7);
                        }

                        if (!reader.IsDBNull(8))
                        {
                            profile.SmsGet = reader.GetString(8);
                        }

                        if (!reader.IsDBNull(9))
                        {
                            profile.Email = reader.GetString(9);
                        }

                        if (!reader.IsDBNull(10))
                        {
                            profile.CreateDate = reader.GetDateTime(10);
                        }

                        if (!reader.IsDBNull(11))
                        {
                            profile.ReceivingDate = reader.GetDateTime(11);
                        }

                        if (!reader.IsDBNull(12))
                        {
                            profile.AppointmentDate = reader.GetDateTime(12);
                        }

                        if (!reader.IsDBNull(13))
                        {
                            profile.Object = reader.GetBoolean(13);
                        }

                        if (!reader.IsDBNull(14))
                        {
                            profile.SettlementDate = reader.GetDateTime(14);
                        }

                        if (!reader.IsDBNull(15))
                        {
                            profile.Notes = reader.GetString(15);
                        }

                        if (!reader.IsDBNull(16))
                        {
                            profile.Attachments = reader.GetString(16);
                        }

                        if (!reader.IsDBNull(17))
                        {
                            profile.ContentWork = reader.GetString(17);
                        }

                        if (!reader.IsDBNull(18))
                        {
                            profile.IsDelete = reader.GetBoolean(18);
                        }

                        if (!reader.IsDBNull(19))
                        {
                            profile.OrganName = reader.GetString(19);
                        }

                        if (!reader.IsDBNull(20))
                        {
                            profile.Address = reader.GetString(20);
                        }

                        if (!reader.IsDBNull(21))
                        {
                            profile.Phone = reader.GetString(21);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return profile;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Workflow
        /// </summary>
        public WorkflowEN Workflow
        {
            get
            {
                try
                {
                    WorkflowEN workflow = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowGetById, this.WorkflowId);

                    while (reader.Read())
                    {
                        workflow = new WorkflowEN();

                        if (!reader.IsDBNull(0))
                        {
                            workflow.WorkflowId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            workflow.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            workflow.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            workflow.CreateDate = reader.GetDateTime(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            workflow.Descriptions = reader.GetString(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            workflow.Active = reader.GetBoolean(5);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return workflow;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Node
        /// </summary>
        public NodeEN Node
        {
            get
            {
                try
                {
                    NodeEN node = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeGetById, this.NodeId);

                    while (reader.Read())
                    {
                        node = new NodeEN();

                        if (!reader.IsDBNull(0))
                        {
                            node.NodeId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            node.NodeTypeId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            node.Code = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            node.Name = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            node.Orders = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return node;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets SendUser
        /// </summary>
        public UserEN SendUser
        {
            get
            {
                try
                {
                    UserEN user = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUsersGetById, this.SendUserId);

                    while (reader.Read())
                    {
                        user = new UserEN();

                        if (!reader.IsDBNull(0))
                        {
                            user.UserId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            user.AdministrativeUnitsId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            user.UserName = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            user.FullName = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            user.Sex = reader.GetBoolean(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            user.BirthDate = reader.GetDateTime(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            user.Email = reader.GetString(6);
                        }

                        if (!reader.IsDBNull(7))
                        {
                            user.Sms = reader.GetString(7);
                        }

                        if (!reader.IsDBNull(8))
                        {
                            user.Phone = reader.GetString(8);
                        }

                        if (!reader.IsDBNull(9))
                        {
                            user.Address = reader.GetString(9);
                        }

                        if (!reader.IsDBNull(10))
                        {
                            user.CreateDate = reader.GetDateTime(10);
                        }

                        if (!reader.IsDBNull(11))
                        {
                            user.Send = reader.GetString(11);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return user;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets MainUser
        /// </summary>
        public UserEN MainUser
        {
            get
            {
                try
                {
                    UserEN user = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUsersGetById, this.MainUserId);

                    while (reader.Read())
                    {
                        user = new UserEN();

                        if (!reader.IsDBNull(0))
                        {
                            user.UserId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            user.AdministrativeUnitsId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            user.UserName = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            user.FullName = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            user.Sex = reader.GetBoolean(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            user.BirthDate = reader.GetDateTime(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            user.Email = reader.GetString(6);
                        }

                        if (!reader.IsDBNull(7))
                        {
                            user.Sms = reader.GetString(7);
                        }

                        if (!reader.IsDBNull(8))
                        {
                            user.Phone = reader.GetString(8);
                        }

                        if (!reader.IsDBNull(9))
                        {
                            user.Address = reader.GetString(9);
                        }

                        if (!reader.IsDBNull(10))
                        {
                            user.CreateDate = reader.GetDateTime(10);
                        }

                        if (!reader.IsDBNull(11))
                        {
                            user.Send = reader.GetString(11);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return user;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets SendDepartment
        /// </summary>
        public DepartmentEN SendDepartment
        {
            get
            {
                try
                {
                    DepartmentEN department = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDepartmentGetById, this.SendDepartmentId);

                    while (reader.Read())
                    {
                        department = new DepartmentEN();

                        if (!reader.IsDBNull(0))
                        {
                            department.DepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            department.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            department.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            department.Parent = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            department.Order = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return department;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets SendDepartment
        /// </summary>
        public DepartmentEN MainDepartment
        {
            get
            {
                try
                {
                    DepartmentEN department = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDepartmentGetById, this.MainDepartmentId);

                    while (reader.Read())
                    {
                        department = new DepartmentEN();

                        if (!reader.IsDBNull(0))
                        {
                            department.DepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            department.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            department.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            department.Parent = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            department.Order = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return department;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets ProgressWork
        /// </summary>
        public ProgressWorkEN ProgressWork
        {
            get
            {
                try
                {
                    ProgressWorkEN progressWork = null;
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkGetById, this.ProgressWorkId);
                    while (reader.Read())
                    {
                        progressWork = new ProgressWorkEN();
                        if (!reader.IsDBNull(0))
                        {
                            progressWork.ProgressWorkId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            progressWork.ProfileTypeProgressId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            progressWork.ProfileId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            progressWork.FinishDate = reader.GetDateTime(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            progressWork.Status = reader.GetString(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            progressWork.Time = reader.GetString(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            progressWork.StartDate = reader.GetDateTime(6);
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                    return progressWork;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Coordinations
        /// </summary>
        public IList<CoordinationEN> Coordinations
        {
            get
            {
                try
                {
                    IList<CoordinationEN> lstCoordinations = new List<CoordinationEN>();
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneCoordinationGetByWorkflowProfileId, this.WorkflowProfileId);
                    while (reader.Read())
                    {
                        CoordinationEN coordination = new CoordinationEN();
                        if (!reader.IsDBNull(0))
                        {
                            coordination.CoordinationId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            coordination.CoordinationUserId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            coordination.WorkflowProfileId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            coordination.Role = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            coordination.RW = reader.GetBoolean(4);
                        }

                        lstCoordinations.Add(coordination);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstCoordinations;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
